package DML;

import java.util.ArrayList;
import java.util.List;

import DuiXiang.TableModel;
import DDL.GetTableMessage;
import DML.util;

public class SelectTable {

	public static int selectDate(String dataBaseName,
			String tableName, List<String> name, List<String> values,
			List<String> needName, List<List<String>> resultSet,boolean all) {
		int i, number;
		int[] temp = new int[20];
		List<List<String>> te = new ArrayList<List<String>>();
		String dbfPath = "D:\\database\\" + dataBaseName + "\\" + tableName
				+ ".dbf";
		String datPath = "D:\\database\\" + dataBaseName + "\\" + tableName
				+ ".dat";
		List<TableModel> tableModelList = util.ReadTableModelList(dbfPath);
		List<List<String>> tableContent = util.readTable(datPath);
		if (tableContent == null)
			return -1; // 无该表或数据库
		if(!all){
		number = 0;
		for (String nn : name) {
			i = 0;
			for (TableModel aa : tableModelList) {
				if (aa.getsFieldName().equals(nn))
					break;
				i++;
			}
			temp[number++] = i;
		}
		for (List<String> aline : tableContent) {
			for (i = 0; i < number; i++) {
				String a = aline.get(temp[i]);
				String b = values.get(i);
				if (!a.equals(b))
					break;
			}
			if (i == number)
				te.add(aline);
		}
		if (te.isEmpty())
			return -5; // 无索要查找的元祖
		}
		else te=tableContent;
		number = 0;
		for (String nn : needName) {
			i = 0;
			for (TableModel aa : tableModelList) {
				if (aa.getsFieldName().equals(nn))
					break;
				i++;
			}
			temp[number++] = i;
		}
		
		List<String>  attrList=new ArrayList<String>();
		GetTableMessage.getTableMessage(dataBaseName, tableName, attrList);
		resultSet.add(attrList);
		
		List<String> tem = new ArrayList<String>();
		for (List<String> aline : te) {
			for (i = 0; i < number; i++) {
				tem.add(aline.get(temp[i]));
			}
			resultSet.add(aline);
		}
		return 1;
	}
}
